package cn.sdormitory.canteen.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.sdormitory.canteen.dao.CanteenStockDetailDao;
import cn.sdormitory.canteen.dao.FoodSupplierGoodsDao;
import cn.sdormitory.canteen.service.FoodSupplierGoodsService;
import cn.sdormitory.canteen.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service("foodSupplierGoodsService")
public class FoodSupplierGoodsServiceImpl extends ServiceImpl<FoodSupplierGoodsDao, FoodSupplierGoodsVO> implements FoodSupplierGoodsService {

    @Autowired
    private FoodSupplierGoodsDao foodSupplierGoodsDao;
    @Override
    public IPage<FoodSupplierGoodsVO> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

        LambdaQueryWrapper<FoodSupplierGoodsVO> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(params.get("goodsName"))) {
            String goodsName = String.valueOf(params.get("goodsName"));
            wrapper.likeRight(FoodSupplierGoodsVO::getGoodsName, goodsName);
        }
        if (!StringUtils.isEmpty(params.get("supplierId"))) {
            String supplierId = String.valueOf(params.get("supplierId"));
            wrapper.eq(FoodSupplierGoodsVO::getSupplierId, supplierId);
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public int saveOrUpdateFoodSupplierGoodsInfo(FoodSupplierGoodsVO vo) {
        int num = 0;
        if(StringUtils.isEmpty(vo.getId())){
            num = foodSupplierGoodsDao.insert(vo);
        }else{
            num = foodSupplierGoodsDao.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteFoodSupplierGoodsById(Long id) {
        return foodSupplierGoodsDao.deleteById(id);
    }


}
